//
//  HMHomeRetweetView.swift
//  Weibo
//
//  Created by Apple on 2017/7/13.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SnapKit
import YYText

//  转发微博视图
class HMHomeRetweetView: UIView {
    
    //  记录转发微博的底部约束
    var retweetBottomConstraint: Constraint?

    //  引用ViewModel
    var statusViewModel: HMStatusViewModel? {
        didSet {
//            contentLabel.text = statusViewModel?.retweetContent
            contentLabel.attributedText = statusViewModel?.retweetAttributeStr
            
            //  卸载上次约束
            retweetBottomConstraint?.deactivate()
            
            //  判断是否有配图数据
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  有配图
                pictureView.isHidden = false
                self.snp.makeConstraints({ (make) in
                    retweetBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  绑定数据
                pictureView.picUrls = picUrls
            } else {
                //  没有配图
                pictureView.isHidden = true
                //  重写创建底部约束
                self.snp.makeConstraints({ (make) in
                    retweetBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
                
            }
            
            
        }
    }
    
    //  MARK:   --懒加载控件
    private lazy var contentLabel: YYLabel = {
//        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        let label = YYLabel()
        //  最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        label.numberOfLines = 0
        return label
    }()
    
    //  转发微博配图
    private lazy var pictureView: HMHomePictureView = {
        let pictureView = HMHomePictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(HomeCellMargin).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //  设置约束优先级
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        self.snp.makeConstraints { (make) in
            retweetBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
        contentLabel.highlightTapAction = { (containerView, attributedText, range, rect) in
            let result = (attributedText.string as NSString).substring(with: range)
            print(result)
        }
    }

}
